
import axios from "axios";

export const SERVER_HOST = "http://chat.helapu.com"

const instance = axios.create({
  timeout: 10000,
  baseURL: SERVER_HOST
})

instance.defaults.headers.post['Content-Type'] = 'application/json'

let httpCode = {
  400: '请求参数错误',
  401: '权限不足, 请重新登录',
  403: '服务器拒绝本次访问',
  404: '请求资源未找到',
  500: '内部服务器错误',
  501: '服务器不支持该请求中使用的方法',
  502: '网关错误',
  504: '网关超时'
}

instance.interceptors.request.use(config => {
  config.headers["Authorization"] = localStorage.getItem('token') || ''

  if (config.url.includes('pur/contract/export')) {
      config.headers['responseType'] = 'blob'
  }
  
  if (config.url.includes('pur/contract/upload')) {
      config.headers['Content-Type'] = 'multipart/form-data'
  }
  return config
}, error => {
  return Promise.reject(error)
})


instance.interceptors.response.use(response => {
    
  console.log("response " + response.status);
  return response.data

}, error => {
  console.log("response error " + error);
})



export const get = (url, params, config = {}) => {
  return new Promise((resolve, reject) => {
    instance({
      method: 'get',
      url,
      params,
      ...config
    }).then(response => {
      resolve(response)
    }).catch(error => {
      reject(error)
    })
  })
}

export const post = (url, data, config = {}) => {
  return new Promise((resolve, reject) => {
    instance({
      method: 'post',
      url,
      data,
        ...config
    }).then(response => {
      resolve(response)
    }).catch(error => {
      reject(error)
    })
  })
}


export default instance
